package com.thoughtworks.biblioteca;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Formatter;
import java.io.*;

/**
 * Command design pattern. It is the receiver to really perform the command.
 * Read the movies to memory from the file and hold them in an ArrayList.
 */
public class MovieLibrary {

    private final static String MOVIE_LIBRARY_PATH = "/resource/Movies.lib";

    private ArrayList<Movie> movieLibrary;

    public MovieLibrary() {
        movieLibrary = new ArrayList<Movie>();
        loadMoviesFromFile(this.getClass().getResource("/").getPath()
                + MOVIE_LIBRARY_PATH);
    }

    /*
      * load movies from the file to memory.
      */
    private boolean loadMoviesFromFile(String movieLibraryFilePath) {
        File moviesFile = new File(movieLibraryFilePath);
        BufferedReader reader = null;
        if (moviesFile.exists()) {
            try {
                reader = new BufferedReader(new InputStreamReader(
                        new FileInputStream(moviesFile)));
                String movieInfo;
                String[] movieInfoArray;
                while ((movieInfo = reader.readLine()) != null
                        && !"".equals(movieInfo)) {
                    movieInfoArray = movieInfo.split(",");
                    movieLibrary.add(new Movie(movieInfoArray[0].trim(),
                            movieInfoArray[1].trim(), Integer
                            .parseInt(movieInfoArray[2].trim())));
                }
            } catch (FileNotFoundException e) {
                System.out
                        .println("Don't find the movie library file: Movies.lib.");
                return false;
            } catch (IOException e) {
                System.out.println("Something wrong with the IO.");
                return false;
            } finally {
                try {
                    if (reader != null) {
                        reader.close();
                    }
                } catch (IOException e) {
                    System.out.println("Something wrong with the IO.");
                    return false;
                }
            }
            return true;
        } else {
            System.out
                    .println("Don't find the movie library file: Movies.lib.");
            return false;
        }
    }

    /*
      * list the movies to screen and sort the movie by rating.
      */
    public boolean listMovieCatalogToScreen() {
        Formatter formatter = new Formatter();
        formatter.format("%-20s %-20s %-20s\n", "Movie name", "Director",
                "Rating");
        Collections.sort(movieLibrary, new RatingComparator());
        for (Movie movie : movieLibrary) {
            if (movie.getRating() == 0) {
                formatter.format("%-20s %-20s %-20s\n", movie.getMovieName(), movie
                        .getDirector(), "N/A");
            } else {
                formatter.format("%-20s %-20s %-20s\n", movie.getMovieName(), movie
                        .getDirector(), movie.getRating());
            }
        }
        System.out.println(formatter);
        return true;
    }
}
